package com.lemon.smartsafety.http.service;

import android.content.Context;
import android.util.Log;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.ApplicationInfo;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.entity.bind.BindVo;
import com.lemon.smartsafety.http.UrlInfo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.utils.app.UserTypeHelper;
import com.lemon.smartsafety.utils.common.AppUtils;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.safecloud.greendao.PlayView;
import com.lemon.safecloud.greendao.PlayViewDao;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import static com.lemon.smartsafety.app.EventTag.TAG_MAIN_PANEL_AD;

/**
 * Created by Cooliean on 16/1/19.
 */
public class MainFragmentServices extends BaseHttpServices {
    private static final String       TAG          = "MainFragmentServices";
    private              LoggerHelper loggerHelper = new LoggerHelper(MainFragmentServices.class);


    private MainFragmentServices(Context context) {
        super(context);
    }

    /**
     * 初始化，等同使用构造函数
     *
     * @return
     */
    public static MainFragmentServices init() {
        return new MainFragmentServices(IApplication.context());
    }

    /**
     * 初始化，等同使用构造函数
     *
     * @param context
     * @return
     */
    public static MainFragmentServices init(Context context) {
        return new MainFragmentServices(context);
    }

    public MainFragmentServices getMainPanelAd() {
        LemonRequestParams params = new LemonRequestParams();
        params.put("page_size", "6");
        params.put("current_page", "1");
        params.put("column_id", "");
        getMainPanelAd(params);
        return this;
    }

    public MainFragmentServices getMainPanelAd(LemonRequestParams params) {
        HandlerCallback<List<PlayView>> callback = new HandlerCallback<List<PlayView>>(TAG_MAIN_PANEL_AD) {
        };
        callback.localCallBack(getAllLocalADInfo());
        if (!AppUtils.isNetworkAvailable(IApplication.context())) {//如果网络不正常,这里就获取本地
            return this;
        }
        callback.setInnerCallBack(new HandlerCallback.InnerCallBack<List<PlayView>>() {
            @Override
            public void onInnerCallBack(List<PlayView> data) {
                saveADInfo(data);
            }
        });
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.URL_MAIN_PANEL_AD);
        OkHttpUtils.getInstance().post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }

    /**
     * 获取广告的本地数据
     *
     * @return
     */
    public List<PlayView> getAllLocalADInfo() {
        PlayViewDao    dao    = AppService.getDBHelper().getDaoSession().getPlayViewDao();
        List<PlayView> result = dao.queryBuilder().list();
        if (result == null) {
            return new ArrayList<PlayView>();
        }
        return result;
    }

    /**
     * 缓存广告类型
     *
     * @param items
     * @return
     */
    public MainFragmentServices saveADInfo(List<PlayView> items) {
        PlayViewDao dao = AppService.getDBHelper().getDaoSession().getPlayViewDao();
        try {
            if (items != null && !items.isEmpty()) {
                dao.insertOrReplaceInTx(items);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }


    /**
     * 自动根据用户类型获取绑定信息
     *
     * @param applicationCode
     * @return
     */
    public MainFragmentServices getBindInfo(String applicationCode) {
        if (UserTypeHelper.isGovUser()) {
            getGovBindInfo(applicationCode);
        } else {
            getComBindInfo(applicationCode);
        }
        return this;
    }


    /**
     * 自动根据用户类型获取绑定信息
     *
     * @param applicationCode
     * @return
     */
    public MainFragmentServices getBindInfo(ApplicationInfo.ApplicationCode applicationCode) {
        if (UserTypeHelper.isGovUser()) {
            getGovBindInfo(applicationCode.getCode());
        } else {
            getComBindInfo(applicationCode.getCode());
        }
        return this;
    }

    /**
     * 自动根据用户类型获取绑定信息
     *
     * @param applicationCode
     * @return
     */
    public MainFragmentServices getBindInfo(ApplicationInfo.ApplicationCode applicationCode,String tag) {
        if (UserTypeHelper.isGovUser()) {
            getBindInfo(UrlInfo.URL_GOV_BIND_INFO, applicationCode.getCode(),tag);
        } else {
            getBindInfo(UrlInfo.URL_COM_BIND_INFO, applicationCode.getCode(),tag);
        }
        return this;
    }

    /**
     * 获取企业绑定信息
     *
     * @param applicationCode
     * @return BindVo.json
     */
    public MainFragmentServices getComBindInfo(String applicationCode) {
        getBindInfo(UrlInfo.URL_COM_BIND_INFO, applicationCode,EventTag.TAG_BIND_INFO_HTTP);
        return this;
    }

    /**
     * 获取政府机关的绑定信息
     *
     * @param applicationCode
     * @return BindVo.json
     */
    public MainFragmentServices getGovBindInfo(String applicationCode) {
        getBindInfo(UrlInfo.URL_GOV_BIND_INFO, applicationCode,EventTag.TAG_BIND_INFO_HTTP);
        return this;
    }
    /**
     * 获取绑定信息
     *
     * @param applicationCode
     * @return
     */
    private MainFragmentServices getBindInfo(String url, String applicationCode,String tag) {
        LemonRequestParams params = new LemonRequestParams();
        params.putUserId(this.mContext);
        params.put("application_code", applicationCode);
        HandlerCallback<BindVo> callback = new HandlerCallback<BindVo>(tag) {
        };
        callback.setOther(applicationCode);//传递功能代码
        String serverUrl = mSharedPreferencesServer.getRequestUrl(url);
        Log.v("Url:",serverUrl);
        OkHttpUtils.getInstance().post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }

}
